<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="智红的博客">
    <meta name="keyword"  content="智红">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        java学习5_IO框架 - 智红的博客
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/css/aircloud.css">

    
<link rel="stylesheet" href="/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 5.3.0"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 好记性不如烂键盘呐 </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>Zhihong Li</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archive">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%B5%81%E7%9A%84%E5%88%86%E7%B1%BB"><span class="toc-text">流的分类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8C%89%E6%96%B9%E5%90%91%E3%80%90%E9%87%8D%E7%82%B9%E3%80%91"><span class="toc-text">按方向【重点】</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8C%89%E5%8D%95%E4%BD%8D"><span class="toc-text">按单位</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8C%89%E5%8A%9F%E8%83%BD"><span class="toc-text">按功能</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AD%97%E8%8A%82%E6%B5%81"><span class="toc-text">字节流</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E8%8A%82%E6%B5%81%E7%9A%84%E7%88%B6%E7%B1%BB%EF%BC%88%E6%8A%BD%E8%B1%A1%E7%B1%BB%EF%BC%89"><span class="toc-text">字节流的父类（抽象类）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E8%8A%82%E6%B5%81%E7%9A%84%E5%AD%90%E7%B1%BB"><span class="toc-text">字节流的子类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E5%AD%97%E8%8A%82%E6%B5%81"><span class="toc-text">文件字节流</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E5%AD%97%E8%8A%82%E6%B5%81%E5%B0%8F%E6%A1%88%E4%BE%8B"><span class="toc-text">文件字节流小案例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E8%8A%82%E7%BC%93%E5%86%B2%E6%B5%81"><span class="toc-text">字节缓冲流</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AF%B9%E8%B1%A1%E6%B5%81"><span class="toc-text">对象流</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BA%8F%E5%88%97%E5%8C%96"><span class="toc-text">序列化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96"><span class="toc-text">反序列化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-text">序列化和反序列化注意事项</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BC%96%E7%A0%81%E6%96%B9%E5%BC%8F"><span class="toc-text">编码方式</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%B5%81"><span class="toc-text">字符流</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%B5%81%E7%9A%84%E7%88%B6%E7%B1%BB%EF%BC%88%E6%8A%BD%E8%B1%A1%E7%B1%BB%EF%BC%89%EF%BC%9A"><span class="toc-text">字符流的父类（抽象类）：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%B5%81%E7%9A%84%E5%AD%90%E7%B1%BB"><span class="toc-text">字符流的子类</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E6%B5%81%E5%B0%8F%E6%A1%88%E4%BE%8B"><span class="toc-text">字符流小案例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E7%BC%93%E5%86%B2%E6%B5%81"><span class="toc-text">字符缓冲流</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%93%E5%8D%B0%E6%B5%81"><span class="toc-text">打印流</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BD%AC%E6%8D%A2%E6%B5%81"><span class="toc-text">转换流</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#File%E7%B1%BB"><span class="toc-text">File类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C"><span class="toc-text">文件操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E5%A4%B9%E6%93%8D%E4%BD%9C"><span class="toc-text">文件夹操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-text">文件过滤器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C%E5%B0%8F%E6%A1%88%E4%BE%8B"><span class="toc-text">文件操作小案例</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E8%A1%A5%E5%85%85%EF%BC%9AProperties"><span class="toc-text">补充：Properties</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> 好记性不如烂键盘呐 </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        java学习5_IO框架
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2021-01-25 19:48:46</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#JAVA" title="JAVA">JAVA</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Tz4y1X7H7">千锋教育-2020年最新版 Java基础-IO框架</a></p>
<p><a target="_blank" rel="noopener" href="https://lazydog036.gitee.io/2020/11/02/JAVA%20IO%E6%A1%86%E6%9E%B6/" title="Permalink to JAVA IO框架 |  个人博客">Source</a></p>
<ul>
<li><strong>概念</strong>：内存与存储设备之间传输数据的通道。</li>
</ul>
<p><img src="./java%E5%AD%A6%E4%B9%A0/java%E5%AD%A6%E4%B9%A05_01.png" alt=""></p>
<ul>
<li>水借助管道传输；数据借助流传输。</li>
</ul>
<h1 id="流的分类"><a href="#流的分类" class="headerlink" title="流的分类"></a><strong>流的分类</strong></h1><h2 id="按方向【重点】"><a href="#按方向【重点】" class="headerlink" title="按方向【重点】"></a><strong>按方向【重点】</strong></h2><ul>
<li><strong>输入流</strong>：将<code>存储设备</code>中的内容读入到<code>内存</code>中。</li>
<li><strong>输出流</strong>：将<code>内存</code>中的内容写入到<code>存储设备</code>中。</li>
</ul>
<p><img src="./java%E5%AD%A6%E4%B9%A0/java%E5%AD%A6%E4%B9%A05_02.png" alt=""></p>
<h2 id="按单位"><a href="#按单位" class="headerlink" title="按单位"></a><strong>按单位</strong></h2><ul>
<li><strong>字节流</strong>：以字节为单位，可以读写所有数据。</li>
<li><strong>字符流</strong>：以字符为单位，只能读写文本数据。</li>
</ul>
<h2 id="按功能"><a href="#按功能" class="headerlink" title="按功能"></a><strong>按功能</strong></h2><ul>
<li><strong>字节流</strong>：具有实际传输数据的读写功能。</li>
<li><strong>过滤流</strong>：在节点流的基础之上增强功能。</li>
</ul>
<h1 id="字节流"><a href="#字节流" class="headerlink" title="字节流"></a><strong>字节流</strong></h1><p><img src="./java%E5%AD%A6%E4%B9%A0/java%E5%AD%A6%E4%B9%A05_03.png" alt=""></p>
<h2 id="字节流的父类（抽象类）"><a href="#字节流的父类（抽象类）" class="headerlink" title="字节流的父类（抽象类）"></a><strong>字节流的父类（抽象类）</strong></h2><ul>
<li><strong>InputStream</strong>字节输入流<ul>
<li><code>public int read()&#123;&#125;</code></li>
<li><code>public int read(byte[] b)&#123;&#125;</code></li>
<li><code>public int read(byte[] b,int off,int len)&#123;&#125;</code></li>
</ul>
</li>
<li><strong>OutputStream</strong>字节输出流<ul>
<li><code>public void write(int n)&#123;&#125;</code></li>
<li><code>public void write(byte[] b)&#123;&#125;</code></li>
<li><code>public void write(byte[] b,int off,int len)&#123;&#125;</code></li>
</ul>
</li>
</ul>
<h2 id="字节流的子类"><a href="#字节流的子类" class="headerlink" title="字节流的子类"></a><strong>字节流的子类</strong></h2><h3 id="文件字节流"><a href="#文件字节流" class="headerlink" title="文件字节流"></a><strong>文件字节流</strong></h3><ul>
<li><p><strong>FileInputStream</strong></p>
<ul>
<li><code>public int read()</code>//从输入流中读取一个字节数据，返回读到的字节数据，如果达到文件末尾，返回-1。</li>
<li><code>public int read(byte[] b)</code>//从输入流中读取字节数组长度的字节数据存入数组中，返回实际读到的字节数；如果达到文件的尾部，则返回-1。</li>
</ul>
</li>
<li><p><strong>FileOutputStream</strong></p>
<ul>
<li><code>public void write(int b)</code>//将指定字节写入输出流。</li>
<li><code>public void write(bute[] b)</code>//一次写多个字节，将b数组中所有字节，写入输出流。</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 演示文件字节输入流的使用</span></span><br><span class="line"><span class="comment"> * FileInputStream</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo1</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		<span class="comment">//文件内容：abcdefg</span></span><br><span class="line">		FileInputStream fileInputStream=<span class="keyword">new</span> FileInputStream(<span class="string">&quot;d:\\aaa.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//read();读入单个字节</span></span><br><span class="line">		<span class="keyword">int</span> data=fileInputStream.read();</span><br><span class="line">		System.out.println((<span class="keyword">char</span>)data);<span class="comment">//a</span></span><br><span class="line">		<span class="keyword">while</span>((data=fileInputStream.read())!=-<span class="number">1</span>) &#123;</span><br><span class="line">			System.out.print((<span class="keyword">char</span>)data);</span><br><span class="line">		&#125;<span class="comment">//bcdefg</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//read(byte[] b);读入多个字节</span></span><br><span class="line">		<span class="keyword">byte</span>[] b=<span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];	</span><br><span class="line">		<span class="keyword">if</span>((data=fileInputStream.read(b))!=-<span class="number">1</span>) &#123;</span><br><span class="line">			System.out.println(<span class="keyword">new</span> String(b,<span class="number">0</span>,data));</span><br><span class="line">		&#125;<span class="comment">//bcdefg</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//关闭流</span></span><br><span class="line">		fileInputStream.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 演示文件字节输出流的使用</span></span><br><span class="line"><span class="comment"> * FileOutputStream</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo2</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		<span class="comment">//如果没有文件会自动创建</span></span><br><span class="line">		FileOutputStream fileOutputStream=<span class="keyword">new</span> FileOutputStream(<span class="string">&quot;d:\\bbb.txt&quot;</span>);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//write(int b);</span></span><br><span class="line">		fileOutputStream.write(<span class="number">97</span>);</span><br><span class="line">		fileOutputStream.write(<span class="string">&#x27;b&#x27;</span>);</span><br><span class="line">		fileOutputStream.write(<span class="string">&#x27;c&#x27;</span>);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//write(byte[] b);</span></span><br><span class="line">		fileOutputStream.write(<span class="keyword">new</span> String(<span class="string">&quot;helloworld&quot;</span>).getBytes());</span><br><span class="line">		<span class="comment">//此时文件bbb.txt内容为abc	helloworld</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">//关闭流</span></span><br><span class="line">		fileOutputStream.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="文件字节流小案例"><a href="#文件字节流小案例" class="headerlink" title="文件字节流小案例"></a><strong>文件字节流小案例</strong></h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用文件字节流复制文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo3</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		<span class="comment">//将图片读取到输入流</span></span><br><span class="line">		FileInputStream fileInputStream=<span class="keyword">new</span> FileInputStream(<span class="string">&quot;d:\\MrG1.jpg&quot;</span>);</span><br><span class="line">		<span class="comment">//从输出流写入数据</span></span><br><span class="line">		FileOutputStream fileOutputStream=<span class="keyword">new</span> FileOutputStream(<span class="string">&quot;d:\\MrG2.jpg&quot;</span>);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">int</span> count;<span class="comment">//保存一次读取到的实际个数</span></span><br><span class="line">		<span class="keyword">byte</span>[] b=<span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line"></span><br><span class="line">		<span class="keyword">while</span>((count=fileInputStream.read(b))!=-<span class="number">1</span>) &#123;</span><br><span class="line">			fileOutputStream.write(b, <span class="number">0</span>, count);</span><br><span class="line">		&#125;</span><br><span class="line">		System.out.println(<span class="string">&quot;复制成功&quot;</span>);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//关闭流</span></span><br><span class="line">		fileInputStream.close();</span><br><span class="line">		fileOutputStream.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="字节缓冲流"><a href="#字节缓冲流" class="headerlink" title="字节缓冲流"></a><strong>字节缓冲流</strong></h3><ul>
<li><p><strong>缓冲流</strong>：BufferedInputStream/BufferedOutputStream</p>
<ul>
<li>提高IO效率，减少访问磁盘的次数；</li>
<li>数据存储在缓冲区中。flush可以将缓存区的内容写入文件，也可以直接close。</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用字节缓冲流读取</span></span><br><span class="line"><span class="comment"> * BufferedInputStream</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo4</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="comment">//使用该输入流每次会从硬盘读入</span></span><br><span class="line">        FileInputStream fileInputStream=<span class="keyword">new</span> FileInputStream(<span class="string">&quot;d:\\aaa.txt&quot;</span>);</span><br><span class="line">        <span class="comment">//缓冲流需要一个底层流</span></span><br><span class="line">        <span class="comment">//缓冲流每次从缓冲区读取</span></span><br><span class="line">        BufferedInputStream bufferedInputStream=<span class="keyword">new</span> BufferedInputStream(fileInputStream);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//读取</span></span><br><span class="line">        <span class="keyword">int</span> data; </span><br><span class="line">        <span class="keyword">while</span>((data=bufferedInputStream.read())!=-<span class="number">1</span>) &#123;</span><br><span class="line">            System.out.print((<span class="keyword">char</span>)data); </span><br><span class="line">        &#125;	 </span><br><span class="line"></span><br><span class="line">        <span class="comment">//我们也可以自己创建一个缓冲区；</span></span><br><span class="line">        <span class="comment">//每次读取从自己创建的缓冲区中读取。</span></span><br><span class="line">        <span class="keyword">int</span> count;</span><br><span class="line">        <span class="keyword">byte</span>[] buf=<span class="keyword">new</span> <span class="keyword">byte</span>[<span class="number">1024</span>];</span><br><span class="line">        <span class="keyword">while</span>((count=bufferedInputStream.read(b,<span class="number">0</span>,b.length))!=-<span class="number">1</span>) &#123;			</span><br><span class="line">            System.out.println(<span class="keyword">new</span> String(buf,<span class="number">0</span>,count));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        bufferedInputStream.close();	</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用字节缓冲流写入文件</span></span><br><span class="line"><span class="comment"> * BufferedOutputStream</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo5</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		FileOutputStream fileOutputStream=<span class="keyword">new</span> FileOutputStream(<span class="string">&quot;d:\\buf.txt&quot;</span>);</span><br><span class="line">		<span class="comment">//缓冲流将数据写入到缓冲区</span></span><br><span class="line">		BufferedOutputStream bufferedOutputStream=<span class="keyword">new</span> BufferedOutputStream(fileOutputStream);</span><br><span class="line">		<span class="comment">//写入文件</span></span><br><span class="line">		bufferedOutputStream.write(<span class="string">&quot;helloworld&quot;</span>.getBytes());</span><br><span class="line">		bufferedOutputStream.flush();</span><br><span class="line"></span><br><span class="line">		<span class="comment">//其实内部也会调用flush</span></span><br><span class="line">		bufferedOutputStream.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="对象流"><a href="#对象流" class="headerlink" title="对象流"></a><strong>对象流</strong></h1><ul>
<li><p><strong>对象流</strong>：ObjectOutputStream/ObjectInputStream</p>
<ul>
<li>增加了缓冲区功能。</li>
<li>增强了读写8种基本数据类型和字符串功能。</li>
<li>增强了读写对象的功能：<ul>
<li><code>readObject()</code>//从流中读取一个对象。</li>
<li><code>writeObject(Object obj)</code>向流中写入一个对象。</li>
</ul>
</li>
</ul>
<p><strong>使用流传输对象的过程称为序列化、反序列化。</strong></p>
</li>
</ul>
<h2 id="序列化"><a href="#序列化" class="headerlink" title="序列化"></a><strong>序列化</strong></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 学生类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(String name, <span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>();</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> age; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>&#123; <span class="keyword">this</span>.age = age; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123; <span class="keyword">return</span> name; &#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123; <span class="keyword">this</span>.name = name; &#125;</span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">&quot;Student [name=&quot;</span> + name + <span class="string">&quot;, age=&quot;</span> + age + <span class="string">&quot;]&quot;</span>;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用ObjectOutputStream实现对象的序列化</span></span><br><span class="line"><span class="comment"> * 注：序列化的类必要要实现Serializable接口</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo6</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        <span class="comment">//这个文件后缀名表示二进制文件，但你可以写成其他如obj等任意后缀。</span></span><br><span class="line">		FileOutputStream fileOutputStream=<span class="keyword">new</span> FileOutputStream(<span class="string">&quot;d:\\stu.bin&quot;</span>);</span><br><span class="line">		ObjectOutputStream objectOutputStream=<span class="keyword">new</span> ObjectOutputStream(fileOutputStream);</span><br><span class="line">		<span class="comment">//序列化（写入操作）</span></span><br><span class="line">		Student tang=<span class="keyword">new</span> Student(<span class="string">&quot;唐瑞&quot;</span>, <span class="number">21</span>);</span><br><span class="line">		objectOutputStream.writeObject(tang);</span><br><span class="line">		objectOutputStream.close();</span><br><span class="line">		System.out.println(<span class="string">&quot;序列化完毕&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>注</strong>：执行上述代码后IDE会抛出<code>java.io.NotSerializableException</code>，意思是Student类不能被序列化，需要实现Serializable接口。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//不需要实现任何方法</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> <span class="keyword">implements</span> <span class="title">Serializable</span></span>&#123; &#125;</span><br></pre></td></tr></table></figure>
<p>Serializable其实是一个标志接口，用来标志该类是否可以被序列化。我们进到该接口的源码可以发现里面不含任何属性和抽象方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Serializable</span> </span>&#123; &#125;</span><br></pre></td></tr></table></figure>
<h2 id="反序列化"><a href="#反序列化" class="headerlink" title="反序列化"></a><strong>反序列化</strong></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用ObjectInputStream实现反序列化（读取重构对象）</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo7</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException, ClassNotFoundException </span>&#123;</span><br><span class="line">		FileInputStream fileInputStream=<span class="keyword">new</span> FileInputStream(<span class="string">&quot;d:\\stu.bin&quot;</span>);</span><br><span class="line">		ObjectInputStream objectInputStream=<span class="keyword">new</span> ObjectInputStream(fileInputStream);</span><br><span class="line">		<span class="comment">//读取文件（反序列化）</span></span><br><span class="line">		Student student=(Student) objectInputStream.readObject();</span><br><span class="line"></span><br><span class="line">		objectInputStream.close();</span><br><span class="line"></span><br><span class="line">		System.out.println(<span class="string">&quot;执行完毕&quot;</span>);</span><br><span class="line">		System.out.println(student.toString());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="序列化和反序列化注意事项"><a href="#序列化和反序列化注意事项" class="headerlink" title="序列化和反序列化注意事项"></a><strong>序列化和反序列化注意事项</strong></h2><ul>
<li>序列化类必须实现Serializable接口，前文已经说过。</li>
<li>序列化类中的对象属性也要求实现Serializable接口。也就是说如果Student类中有一个Grad类型的属性<code>private Grad info;</code>那么Grad这个类也要实现Serializable接口。</li>
<li>序列化类中可以添加序列化版本号ID，以保证序列化的类和被序列化的类是同一个类。在上面的代码中我并没有添加序列号版本，虽然IDE没有报错，但是会显示一个警告，提示我添加序列化版本号（串行版本标识）。我们可以在Student类中添加：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">long</span> serialVersionUID = <span class="number">66666L</span>;</span><br></pre></td></tr></table></figure>
<p>  此时再运行Demo7就会报一个无效类的异常：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java.io.InvalidClassException: local class incompatible: stream classdesc serialVersionUID = -3126921853274410929, local class serialVersionUID = 666660</span><br></pre></td></tr></table></figure>
<p>  意思就是两个类的serialVersionUID不一样。可以看到之前虽然没有显式添加序列版本号，但它已经自动生成了一个。我们再运行一下Demo6序列化，再运行Demo7反序列化就可以正常执行了。</p>
<ul>
<li>使用transient（短暂的）修饰属性，可以避免该属性被序列化。用它来修饰age：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">transient</span> <span class="keyword">int</span> age;</span><br></pre></td></tr></table></figure>
<p>  把tang这个对象序列化后再反序列化，这个对象的age属性就变成了0。</p>
<ul>
<li>静态属性不能被序列化。</li>
<li>可以利用集合来序列化多个对象：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">ArrayList&lt;Student&gt; arrayList=<span class="keyword">new</span> ArrayList&lt;Student&gt;();</span><br><span class="line">arrayList.add(s1);</span><br><span class="line">arrayList.add(s2);</span><br><span class="line">arrayList.add(s3);</span><br><span class="line">objectOutputStream.writeObject(arrayList);</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ArrayList&lt;Student&gt; list=(ArrayList&lt;Student&gt;)objectInputStream.readObject();</span><br></pre></td></tr></table></figure>
<h1 id="编码方式"><a href="#编码方式" class="headerlink" title="编码方式"></a><strong>编码方式</strong></h1><ul>
<li><p>IOS-8859-1</p>
<p>收录除ASCII外，还包括西欧、希腊语、泰语、阿拉伯语、希伯来语对应的文字符号。采用1个字节来表示，最多只能表示256个字符。</p>
</li>
<li><p>UTF-8</p>
<p>针对Unicode码表的可变长度字符编码。国际上使用的编码，也称为“万国码”，收录了几乎所有国家的常用字符。采用1至3个字节来表示一个字符。</p>
</li>
<li><p>GB2312</p>
<p>简体中文，采用1个或2个字节来表示字符，95年之前所采用的编码。</p>
</li>
<li><p>GBK</p>
<p>简体中文的扩充，GB2312的升级版本。</p>
</li>
<li><p>BIG5</p>
<p>台湾，繁体中文。</p>
</li>
</ul>
<p><strong>当编码方式和解码方式不一致时，会出现乱码。</strong>假如Demo1中的文件内容不是字母而是“<strong>我爱中国</strong>”这样的汉字，那么读取出来的信息就是乱码。因为字节流按字节输入输出，而这四个汉字占了12个字节，把一个汉字按一个一个字节读入自然会出现问题，这时就需要使用字符流。</p>
<h1 id="字符流"><a href="#字符流" class="headerlink" title="字符流"></a><strong>字符流</strong></h1><p><img src="./java%E5%AD%A6%E4%B9%A0/java%E5%AD%A6%E4%B9%A05_04.png" alt=""></p>
<h2 id="字符流的父类（抽象类）："><a href="#字符流的父类（抽象类）：" class="headerlink" title="字符流的父类（抽象类）："></a><strong>字符流的父类（抽象类）</strong>：</h2><ul>
<li><p><strong>Reader：</strong>字符输入流</p>
<ul>
<li><p><code>public int read()</code></p>
<p>从流中读取单个字符，用整型来返回读取的字符；当读到流底部时返回-1。</p>
</li>
<li><p><code>public int read(char[] c)</code></p>
<p>从流中读取字符保存到c数组中，返回读取的字符个数，当读到流底部时返回-1。</p>
</li>
<li><p><code>public int read(char[] cbuf,int off,int len)&#123;&#125;</code></p>
<p>抽象方法。</p>
</li>
</ul>
</li>
<li><p><strong>Writer：</strong>字符输出流</p>
<ul>
<li><p><code>public void write(int n)</code></p>
<p>写入单个字符，只能写入包含16位低阶字节的整型数值，16位高阶字节将会被忽略。</p>
</li>
<li><p><code>public void write(String str)</code></p>
<p>写入一个字符串。</p>
</li>
<li><p><code>public void write(char[] cbuf)</code></p>
<p>写入一个字符数组。</p>
</li>
</ul>
</li>
</ul>
<h2 id="字符流的子类"><a href="#字符流的子类" class="headerlink" title="字符流的子类"></a><strong>字符流的子类</strong></h2><ul>
<li><p><strong>FileReader：</strong></p>
<ul>
<li><p><code>public int read()</code></p>
<p>继承自InputStreamReader类。读取单个字符，返回读取的字符，当读到流底部时返回-1。</p>
</li>
<li><p><code>public int read(char[] c)</code></p>
<p>继承自Reader类。</p>
</li>
<li><p><code>public int read(char[] cbuf,int offset,int length)</code></p>
<p>继承自InputStreamReader类。从流中读取部分字符到cbuf中指定位置，返回读取到的字符个数，当读到流底部时返回-1。</p>
</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo1</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;	</span><br><span class="line">		<span class="comment">//文件内容：</span></span><br><span class="line">		<span class="comment">//要是能活在梦里，我情愿醒不过来。</span></span><br><span class="line">		FileReader fileReader=<span class="keyword">new</span> FileReader(<span class="string">&quot;d:\\cbuf.txt&quot;</span>);</span><br><span class="line">		<span class="keyword">int</span> data;</span><br><span class="line">		<span class="comment">//read();与字节流不同，该方法读取单个字符而不是字节</span></span><br><span class="line">		<span class="keyword">while</span>((data=fileReader.read())!=-<span class="number">1</span>) &#123;</span><br><span class="line">			System.out.print((<span class="keyword">char</span>)data);</span><br><span class="line">		&#125;</span><br><span class="line">        <span class="comment">//输出：</span></span><br><span class="line">        <span class="comment">//要是能活在梦里，我情愿醒不过来。</span></span><br><span class="line">		fileReader.close();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> <strong>这里记录我遇到的一个问题</strong>：</p>
<blockquote>
<p>  上述代码执行后理应是正常输出一段文字，但我这里出现了文字乱码。猜测是编码出现了问题，于是查看本地文本的保存格式为UTF-8，感觉也没错，于是打开JDK API查看FileReader类的描述：</p>
<blockquote>
<p>Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.</p>
</blockquote>
<p>  大意就是该类的构造方法有一个默认的字符编码格式和一个默认字节缓冲区，并没有指明这个编码格式就是UTF-8。于是查看系统默认编码，打开CMD输入chcp，得到一个值为936的活动编码页，通过查询得知该代码页所对应的国家（地区）或语言为：中国 - 简体中文(GB2312)。这与本地所保存的文本编码UTF-8不一致，所以导致了文字乱码的出现。而要指定编码格式需要创建一个InputStreamReader或FileInputStream对象使用其构造方法。</p>
</blockquote>
<p>   以下是我在本地能正常运行的代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo1</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;	</span><br><span class="line">       <span class="comment">//指定编码格式</span></span><br><span class="line">        InputStreamReader inputStreamReader=<span class="keyword">new</span> InputStreamReader(<span class="keyword">new</span> FileInputStream(<span class="string">&quot;d:\\cbuf.txt&quot;</span>),<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        <span class="keyword">int</span> data;</span><br><span class="line">        <span class="keyword">while</span>((data=inputStreamReader.read())!=-<span class="number">1</span>) &#123;</span><br><span class="line">            System.out.print((<span class="keyword">char</span>)data);</span><br><span class="line">        &#125;</span><br><span class="line">        inputStreamReader.close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><p><strong>FileWriter:</strong></p>
<ul>
<li><p><code>public void write(int c)</code></p>
<p>继承自OutputStreamWriter类，写入一个字符。</p>
</li>
<li><p><code>public void write(String str)</code></p>
<p>继承自Writer类。</p>
</li>
<li><p><code>public void Write(char[] cbuf)</code></p>
<p>继承自Writer类。</p>
</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用FileWriter写入文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo2</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		FileWriter fileWriter=<span class="keyword">new</span> FileWriter(<span class="string">&quot;d:\\w.txt&quot;</span>);</span><br><span class="line">		fileWriter.write(<span class="string">&quot;给自己一个希望。&quot;</span>);<span class="comment">//写入</span></span><br><span class="line">		fileWriter.close();</span><br><span class="line">		System.out.println(<span class="string">&quot;执行完毕&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="字符流小案例"><a href="#字符流小案例" class="headerlink" title="字符流小案例"></a><strong>字符流小案例</strong></h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用FileReader和FileWrite复制文本文件</span></span><br><span class="line"><span class="comment"> *  注：不能复制图片或二进制文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo3</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		InputStreamReader inputStreamReader=<span class="keyword">new</span> InputStreamReader(<span class="keyword">new</span> FileInputStream(<span class="string">&quot;d:\\w.txt&quot;</span>));</span><br><span class="line">		FileWriter fileWriter=<span class="keyword">new</span> FileWriter(<span class="string">&quot;d:\\w2.txt&quot;</span>);</span><br><span class="line">		<span class="comment">//读写</span></span><br><span class="line">		<span class="keyword">int</span> data=<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">while</span>((data=inputStreamReader.read())!=-<span class="number">1</span>) &#123;</span><br><span class="line">			fileWriter.write(data);			</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		inputStreamReader.close();</span><br><span class="line">		fileWriter.close();</span><br><span class="line">		System.out.println(<span class="string">&quot;执行完毕&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="字符缓冲流"><a href="#字符缓冲流" class="headerlink" title="字符缓冲流"></a><strong>字符缓冲流</strong></h3><ul>
<li><p><strong>缓冲流</strong>：BufferedReader/BufferedWriter</p>
<ul>
<li>高效读写</li>
<li>支持换行输入符</li>
<li>可一次写一行、读一行。</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用字符缓冲流读取文件</span></span><br><span class="line"><span class="comment"> * BufferedReader</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo4</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		InputStreamReader inputStreamReader=<span class="keyword">new</span> InputStreamReader(<span class="keyword">new</span> FileInputStream(<span class="string">&quot;d:\\cbuf.txt&quot;</span>),<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">		BufferedReader bufferedReader=<span class="keyword">new</span> BufferedReader(inputStreamReader);</span><br><span class="line">		<span class="comment">//read(char[] cbuf)</span></span><br><span class="line">		<span class="keyword">char</span>[] cbuf=<span class="keyword">new</span> <span class="keyword">char</span>[<span class="number">1024</span>];</span><br><span class="line">		<span class="keyword">int</span> count;</span><br><span class="line">		<span class="keyword">while</span>((count=bufferedReader.read(cbuf))!=-<span class="number">1</span>) &#123;</span><br><span class="line">			System.out.println(<span class="keyword">new</span> String(cbuf,<span class="number">0</span>,count));</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//readline();</span></span><br><span class="line">		<span class="comment">//该方法一次读取一行，返回一个字符串</span></span><br><span class="line">		String line;</span><br><span class="line">		<span class="keyword">while</span>((line=bufferedReader.readLine())!=<span class="keyword">null</span>) &#123;</span><br><span class="line">			System.out.println(line);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用字符缓冲流写入文件</span></span><br><span class="line"><span class="comment"> * BufferedWriter</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo5</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		BufferedWriter bufferedWriter=<span class="keyword">new</span> BufferedWriter(<span class="keyword">new</span> FileWriter(<span class="string">&quot;d:\\w3.txt&quot;</span>));</span><br><span class="line">		bufferedWriter.write(<span class="string">&quot;真有人看这篇博客吗？&quot;</span>);</span><br><span class="line">		<span class="comment">//写入一个换行符windows \r\n  linux \n</span></span><br><span class="line">		bufferedWriter.newLine();</span><br><span class="line">		bufferedWriter.write(<span class="string">&quot;不会吧不会吧！！&quot;</span>);</span><br><span class="line"></span><br><span class="line">		bufferedWriter.close();</span><br><span class="line">		System.out.println(<span class="string">&quot;执行完毕&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="打印流"><a href="#打印流" class="headerlink" title="打印流"></a><strong>打印流</strong></h3><ul>
<li><p><strong>PrintWriter：</strong></p>
<ul>
<li>封装了print()/println()方法，支持写入后换行。</li>
<li>支持数据原样打印。</li>
</ul>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * PrintWriter的使用</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo6</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> FileNotFoundException </span>&#123;</span><br><span class="line">		PrintWriter printWriter=<span class="keyword">new</span> PrintWriter(<span class="string">&quot;d:\\p.txt&quot;</span>);</span><br><span class="line">		<span class="comment">//打印到文件</span></span><br><span class="line">		printWriter.println(<span class="number">97</span>);<span class="comment">//97</span></span><br><span class="line">		printWriter.println(<span class="string">&#x27;b&#x27;</span>);<span class="comment">//b</span></span><br><span class="line">		printWriter.println(<span class="number">3.14</span>);<span class="comment">//3.14</span></span><br><span class="line">		printWriter.println(<span class="keyword">true</span>);<span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">		printWriter.close();</span><br><span class="line">		System.out.println(<span class="string">&quot;执行完毕&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="转换流"><a href="#转换流" class="headerlink" title="转换流"></a><strong>转换流</strong></h3><ul>
<li><p><strong>桥转换流</strong>：InputStreamReader/OutputStreamWriter</p>
<ul>
<li>可将字节流转换为字符流。</li>
<li>可设置字符的编码方式。</li>
</ul>
<p>其实这个在上面的Demo中我已经用过了，这里不再演示InputStreamReader的使用。</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用OutputStreamWriter写入文件</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo7</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		<span class="comment">//可以自行指定编码</span></span><br><span class="line">		OutputStreamWriter outputStreamWriter=<span class="keyword">new</span> OutputStreamWriter(<span class="keyword">new</span> FileOutputStream(<span class="string">&quot;d:\\info.txt&quot;</span>));</span><br><span class="line">		outputStreamWriter.write(<span class="string">&quot;知道的越多，不明白的也就更多，学海无涯。&quot;</span>);</span><br><span class="line">		outputStreamWriter.close();</span><br><span class="line">		System.out.println(<span class="string">&quot;执行完毕&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="File类"><a href="#File类" class="headerlink" title="File类"></a><strong>File类</strong></h1><ul>
<li><p><strong>概念</strong>：代表物理盘符中的一个<strong>文件</strong>或者<strong>文件夹</strong>。</p>
</li>
<li><p><strong>方法</strong>：</p>
<ul>
<li><p><code>public boolean CreateNewFile()</code></p>
<p>当且仅当指定的文件名不存在时创建一个指定的新的、空的文件。创建成功返回true，如果指定文件名已存在返回false。</p>
</li>
<li><p><code>public boolean mkdir()</code></p>
<p>创建一个指定路径名的文件夹。当且仅当文件夹被创建时返回true，否则返回false。</p>
</li>
<li><p><code>public boolean delete()</code></p>
<p>删除一个指定的文件或文件夹，文件夹必须为空才能被删除。当且仅当指定的文件或文件夹被删除时返回true，否则返回false。</p>
</li>
<li><p><code>public boolean exists()</code></p>
<p>检查指定的文件或文件夹是否存在。当且仅当指定的文件或者文件夹存在时返回true，否则返回false。</p>
</li>
<li><p><code>public File[] listFiles()</code></p>
<p>列出目录中的所有内容，返回一个指定路径名中的文件数组，如果指定的路径名不代表一个文件夹（目录）就返回null。</p>
</li>
<li><p><code>public boolean renameTo(File dest)</code></p>
<p>重命名一个路径名所指定的文件。当且仅当修改操作成功时返回true，否则返回false。</p>
</li>
</ul>
</li>
</ul>
<h2 id="文件操作"><a href="#文件操作" class="headerlink" title="文件操作"></a><strong>文件操作</strong></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 演示文件操作</span></span><br><span class="line"><span class="comment"> * 分隔符</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo1</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException, InterruptedException </span>&#123;</span><br><span class="line">		<span class="comment">//输出：</span></span><br><span class="line">		<span class="comment">//路径分隔符：;</span></span><br><span class="line">		<span class="comment">//名称分隔符：\</span></span><br><span class="line">		separator();</span><br><span class="line"></span><br><span class="line">		fileOp();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">separator</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;路径分隔符：&quot;</span>+File.pathSeparator);</span><br><span class="line">		System.out.println(<span class="string">&quot;名称分隔符：&quot;</span>+File.separator);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">fileOp</span><span class="params">()</span> <span class="keyword">throws</span> IOException, InterruptedException </span>&#123;</span><br><span class="line">		<span class="comment">//1.创建文件</span></span><br><span class="line">		File file=<span class="keyword">new</span> File(<span class="string">&quot;d:\\file.txt&quot;</span>);</span><br><span class="line">		<span class="keyword">if</span>(!file.exists()) &#123;</span><br><span class="line">			<span class="keyword">boolean</span> flag=file.createNewFile();			</span><br><span class="line">			System.out.println(<span class="string">&quot;创建状态：&quot;</span>+flag);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//2.删除文件</span></span><br><span class="line">		<span class="comment">//2.1 直接删除</span></span><br><span class="line">		<span class="comment">/* System.out.println(&quot;删除结果：&quot;+file.delete()); */</span></span><br><span class="line">		<span class="comment">//2.2 使用JVM退出时删除（不是自己删除）</span></span><br><span class="line">		file.deleteOnExit();</span><br><span class="line">		<span class="comment">//调用休眠程序观察删除操作</span></span><br><span class="line">		Thread.sleep(<span class="number">5000</span>);</span><br><span class="line"></span><br><span class="line">		<span class="comment">//3.获取文件信息</span></span><br><span class="line">		System.out.println(<span class="string">&quot;文件绝对路径：&quot;</span>+file.getAbsolutePath());</span><br><span class="line">		System.out.println(<span class="string">&quot;获取路径：&quot;</span>+file.getPath());</span><br><span class="line">		System.out.println(<span class="string">&quot;获取父目录：&quot;</span>+file.getParent());</span><br><span class="line">		System.out.println(<span class="string">&quot;获取文件名称：&quot;</span>+file.getName());</span><br><span class="line">		System.out.println(<span class="string">&quot;获取文件长度：&quot;</span>+file.length());</span><br><span class="line">		System.out.println(<span class="string">&quot;获取文件创建时间：&quot;</span>+<span class="keyword">new</span> Date(file.lastModified()).toLocaleString());</span><br><span class="line"></span><br><span class="line">		<span class="comment">//4.判断</span></span><br><span class="line">		System.out.println(<span class="string">&quot;是否可写：&quot;</span>+file.canWrite());</span><br><span class="line">		System.out.println(<span class="string">&quot;是否可读：&quot;</span>+file.canRead());</span><br><span class="line">		System.out.println(<span class="string">&quot;是否隐藏：&quot;</span>+file.isHidden());</span><br><span class="line">		System.out.println(<span class="string">&quot;是否是文件：&quot;</span>+file.isFile());</span><br><span class="line">		System.out.println(<span class="string">&quot;是否是文件夹：&quot;</span>+file.isDirectory());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="文件夹操作"><a href="#文件夹操作" class="headerlink" title="文件夹操作"></a><strong>文件夹操作</strong></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 文件夹的操作</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo2</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		directorOp();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span>  <span class="title">directorOp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">//1.创建文件夹</span></span><br><span class="line">		File dir=<span class="keyword">new</span> File(<span class="string">&quot;d:\\aaa\\bbb\\ccc&quot;</span>);</span><br><span class="line">		<span class="keyword">if</span> (!dir.exists()) &#123;</span><br><span class="line">			<span class="comment">//mkdir();只能创建单级目录</span></span><br><span class="line">			<span class="keyword">boolean</span> flag=dir.mkdirs();<span class="comment">//创建多级目录</span></span><br><span class="line">			System.out.println(<span class="string">&quot;创建结果：&quot;</span>+flag);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//2.删除文件夹</span></span><br><span class="line">		<span class="comment">//2.1 直接删除（必须为空目录）</span></span><br><span class="line">		<span class="comment">//System.out.println(&quot;删除结果：&quot;+dir.delete());</span></span><br><span class="line">		<span class="comment">//2.2 使用jvm删除</span></span><br><span class="line">		dir.deleteOnExit();</span><br><span class="line"></span><br><span class="line">		<span class="comment">//3.获取文件夹信息</span></span><br><span class="line">		System.out.println(<span class="string">&quot;获取绝对路径：&quot;</span>+dir.getAbsolutePath());</span><br><span class="line">		System.out.println(<span class="string">&quot;获取路径：&quot;</span>+dir.getPath());</span><br><span class="line">		System.out.println(<span class="string">&quot;获取父目录：&quot;</span>+dir.getParent());</span><br><span class="line">		System.out.println(<span class="string">&quot;获取创建时间：&quot;</span>+<span class="keyword">new</span> Date(dir.lastModified()).toLocaleString());</span><br><span class="line">		System.out.println(<span class="string">&quot;文件夹名称：&quot;</span>+dir.getName());</span><br><span class="line"></span><br><span class="line">		<span class="comment">//4.判断</span></span><br><span class="line">		System.out.println(<span class="string">&quot;是否隐藏：&quot;</span>+dir.isHidden());</span><br><span class="line">		System.out.println(<span class="string">&quot;是否是文件：&quot;</span>+dir.isFile());</span><br><span class="line"></span><br><span class="line">		<span class="comment">//5.遍历文件夹</span></span><br><span class="line">		File dir2=<span class="keyword">new</span> File(<span class="string">&quot;d:\\&quot;</span>);</span><br><span class="line">		String[] files=dir2.list();</span><br><span class="line">		<span class="keyword">for</span> (String string : files) &#123;</span><br><span class="line">			System.out.println(string);</span><br><span class="line">		&#125;		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="文件过滤器"><a href="#文件过滤器" class="headerlink" title="文件过滤器"></a><strong>文件过滤器</strong></h2><ul>
<li><p><strong>FileFilter接口：</strong></p>
<p><code>public interface FileFilter</code></p>
<ul>
<li><code>boolean accept(File pathname)</code></li>
<li>当调用File类中的listFiles()方法时，支持传入FileFilter接口实现类，对获取的文件进行过滤，只有满足条件的文件才可以出现在listFiles()的返回值中。</li>
</ul>
<p>在上文Demo2中添加演示代码：</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span>  <span class="title">directorOp</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    File[] files1=dir2.listFiles(<span class="keyword">new</span> FileFilter() &#123;		</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">accept</span><span class="params">(File pathname)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span>(pathname.getName().endsWith(<span class="string">&quot;.txt&quot;</span>)) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="keyword">for</span> (File file : files1) &#123;</span><br><span class="line">        System.out.println(file.getName());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="文件操作小案例"><a href="#文件操作小案例" class="headerlink" title="文件操作小案例"></a><strong>文件操作小案例</strong></h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 1.递归遍历文件夹</span></span><br><span class="line"><span class="comment"> * 2.递归删除文件夹</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo3</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//略</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//案例1：递归遍历文件夹</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">listDer</span><span class="params">(File dir)</span> </span>&#123;</span><br><span class="line">		File[] files=dir.listFiles();</span><br><span class="line">		<span class="keyword">if</span>(files!=<span class="keyword">null</span>&amp;&amp;files.length&gt;<span class="number">0</span>) &#123;</span><br><span class="line">			<span class="keyword">for</span> (File file : files) &#123;</span><br><span class="line">				<span class="keyword">if</span> (file.isDirectory()) &#123;</span><br><span class="line">					listDer(file);</span><br><span class="line">				&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">					System.out.println(file.getName());</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//案例2：递归删除文件夹</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">deleteDir</span><span class="params">(File dir)</span> </span>&#123;</span><br><span class="line">		File[] files=dir.listFiles();</span><br><span class="line">		<span class="keyword">if</span> (files!=<span class="keyword">null</span>&amp;&amp;files.length&gt;<span class="number">0</span>) &#123;</span><br><span class="line">			<span class="keyword">for</span> (File file : files) &#123;</span><br><span class="line">				<span class="keyword">if</span> (file.isDirectory()) &#123;</span><br><span class="line">					deleteDir(file);</span><br><span class="line">				&#125;<span class="keyword">else</span> &#123;</span><br><span class="line">					file.delete();</span><br><span class="line">				&#125;				</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;	</span><br><span class="line">		dir.delete();	</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="补充：Properties"><a href="#补充：Properties" class="headerlink" title="补充：Properties"></a><strong>补充：Properties</strong></h1><ul>
<li><p><strong>Properties：</strong>属性集合</p>
</li>
<li><p><strong>特点：</strong></p>
</li>
<li><p>存储属性名和属性值（键值对）。</p>
<ul>
<li>属性名和属性值都是字符串类型。</li>
<li>没有泛型。</li>
<li>和流有关（所以没有整理在集合里面）。</li>
</ul>
</li>
<li><p><strong>方法：</strong></p>
<ul>
<li><p><code>public String getProperty(String key)</code></p>
<p>根据key在属性列表里查找value，如果原始属性列表找不到就去默认属性列表找。返回key所对应的value。</p>
</li>
<li><p><code>public void list(PrintWriter out)</code></p>
<p>将属性列表打印在指定的输出流上，在debug时很有用。</p>
</li>
<li><p><code>public Object setProperty(String key,String value)</code></p>
<p>内部调用的是Hashtable的put方法，将key和value成对地保存在属性列表中。返回这个key上一个对应的value，没有就返回null。</p>
</li>
</ul>
<p>Properties可以保存在一个流中或是从一个流中加载，属性列表中的每个键值对都是一个字符串。一个属性列表可以包括另一个第二属性列表来作为它的默认值，如果在原始属性列表中没有找到key时就搜索第二属性列表。</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 演示集合properties的使用</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo1</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">		Properties properties=<span class="keyword">new</span> Properties();</span><br><span class="line">		<span class="comment">//添加数据</span></span><br><span class="line">		properties.setProperty(<span class="string">&quot;username&quot;</span>, <span class="string">&quot;tangrui&quot;</span>);</span><br><span class="line">		properties.setProperty(<span class="string">&quot;age&quot;</span>, <span class="string">&quot;21&quot;</span>);</span><br><span class="line">		System.out.println(properties.toString());</span><br><span class="line"></span><br><span class="line">		<span class="comment">//遍历</span></span><br><span class="line">		<span class="comment">//3.1 keySet 略</span></span><br><span class="line">		<span class="comment">//3.2 entrySet 略</span></span><br><span class="line">		<span class="comment">//3.3 stringPropertyNames()</span></span><br><span class="line">		Set&lt;String&gt; set=properties.stringPropertyNames();</span><br><span class="line">		<span class="keyword">for</span> (String string : set) &#123;</span><br><span class="line">			System.out.println(string+<span class="string">&quot; &quot;</span>+properties.getProperty(string));</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="comment">//和流有关的方法</span></span><br><span class="line">		<span class="comment">//list</span></span><br><span class="line">		PrintWriter printWriter=<span class="keyword">new</span> PrintWriter(<span class="string">&quot;d:\\print.txt&quot;</span>);</span><br><span class="line">		properties.list(printWriter);</span><br><span class="line">		printWriter.close();</span><br><span class="line">		<span class="comment">//store保存</span></span><br><span class="line">		FileOutputStream fileOutputStream=<span class="keyword">new</span> FileOutputStream(<span class="string">&quot;d:\\s.properties&quot;</span>);</span><br><span class="line">		properties.store(fileOutputStream, <span class="string">&quot;NOTES&quot;</span>);</span><br><span class="line">		fileOutputStream.close();</span><br><span class="line">		<span class="comment">//load加载</span></span><br><span class="line">		Properties properties2=<span class="keyword">new</span> Properties();</span><br><span class="line">		FileInputStream fileInputStream=<span class="keyword">new</span> FileInputStream(<span class="string">&quot;d:\\s.properties&quot;</span>);</span><br><span class="line">		properties2.load(fileInputStream);</span><br><span class="line">		fileInputStream.close();</span><br><span class="line">		System.out.println(properties2.toString());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        

        

    </ul>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




</html>
